<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleAdd">新增</a-button>
        <a-button preIcon="ant-design:reload-outlined" type="primary" @click="handleRefreshCache">缓存</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="i-ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon style="fontsize: 12px" icon="ant-design:down-outlined" />
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="reload" />
    <Detail @register="registerDetail" />
    <SubjectAuthorize @register="permPolicyDrawer" />
    <HistoryList @register="registerHistoryClient" />
  </div>
</template>
<script lang="ts" name="oauth-client" setup>
  import { ref } from "vue"
  import { BasicTable, TableAction } from "/@/components/Table"
  import InputForm from "./Form.vue"
  import Detail from "./Detail.vue"
  import HistoryList from "./HistoryList.vue"
  import { useGlobSetting } from "/@/hooks/setting"
  import { useDrawer } from "/@/components/Drawer"
  import { columns, searchFormSchema } from "./data"
  import { SubjectAuthorize } from "/@/components/My/SubjectAuthorize/index"
  import { pageClient, deleteOauthClient, batchDeleteOauthClient, refreshCache } from "./api"
  import { useListPage } from "/@/hooks/system/useListPage"
  import { useMessage } from "/@/hooks/web/useMessage"
  import { Icon } from "/@/components/Icon"
  import { useGo } from "/@/hooks/web/usePage"
  import { useModal } from "/@/components/Modal"
  import { isManageOrPlatform } from "/@/utils/tenant"
  import { useUserStore } from "/@/store/modules/user"
  import * as ramApi from "/@/api/sys/ram"
  const go = useGo()
  const { createMessage } = useMessage()
  const glob = useGlobSetting()
  const userStore = useUserStore()

  const [registerDrawer, { openDrawer }] = useDrawer()
  const [permPolicyDrawer, { openDrawer: openPermPolicyrawer }] = useDrawer()
  const [registerDetail, { openDrawer: openDetail }] = useDrawer()
  const [registerHistoryClient, { openModal: openHistoryClientList }] = useModal()
  const { prefixCls, tableContext } = useListPage({
    designScope: "oauth-client",
    tableProps: {
      api: pageClient,
      columns: columns,
      formConfig: {
        labelWidth: 100,
        schemas: searchFormSchema,
      },
      bordered: false,
      actionColumn: {
        width: 150,
        title: "操作",
        dataIndex: "action",
        fixed: "right",
        slots: { customRender: "action" },
      },
    },
  })

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext

  function handleAdd() {
    openDrawer(true, {
      isUpdate: false,
    })
  }

  function handleEdit(record) {
    openDrawer(true, {
      record,
      isUpdate: true,
    })
  }

  async function handleDelete(record) {
    await deleteOauthClient({ id: record.id }, reload)
  }

  function handleAssignPermPolicy(record) {
    openPermPolicyrawer(true, {
       subjectId: record.clientId, 
       subjectName: record.name,
       subjectType: "3",
       config: {
        assignSubjectPermPolicyApi: ramApi.assignPermPolicyToOauthClient,
        success: () => {
          console.log("操作成功")
        }
       }
    })
  }

  async function handleRefreshCache() {
    await refreshCache()
    createMessage.success("刷新成功")
  }

  async function batchHandleDelete() {
    await batchDeleteOauthClient({ ids: selectedRowKeys.value }, reload)
  }

  const handleOpenHistory = (record) => openHistoryClientList(true, { record })
  const handleOpenDetails = (record) => openDetail(true, { record })

  function getActions(record): any[] {
    return [
      {
        label: "编辑",
        onClick: handleEdit.bind(null, record),
        // ifShow: record.sendStatus == 0
      },
      {
        label: "删除",
        ifShow: record.sendStatus != 1,
        color: "error",
        popConfirm: {
          title: "是否确认删除",
          confirm: handleDelete.bind(null, record),
        },
      },
    ]
  }

  function getDropDownAction(record) {
    return [
      {
        label: "详情",
        onClick: handleOpenDetails.bind(null, record),
      },
      {
        label: "授权",
        ifShow: () => isManageOrPlatform(userStore) || userStore.getUserInfo.userName === "admin",
        onClick: handleAssignPermPolicy.bind(null, record),
      },
      {
        label: "历史",
        onClick: handleOpenHistory.bind(null, record),
      },
    ]
  }
</script>
